package com.base.dehong.http;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Type;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;

import com.android.volley.Request.Method;
import com.android.volley.Response.ErrorListener;
import com.android.volley.VolleyError;
import com.base.dehong.util.Config;
import com.base.dehong.util.LogUtils;
import com.base.dehong.util.MD5Util;
import com.base.dehong.util.PreferencesUtils;

/**
 * @author YeFengYun
 * 
 * @describe MD5加密Post请求
 */
public abstract class HttpRequest<T> {
	private Context mContext;
	public TreeMap<String, String> mPostValues = new TreeMap<String, String>();
	RequestCallBack<T> mCallBack;

	public HttpRequest(Context context,RequestCallBack<T> callback) {
		mContext = context;
		mCallBack = callback;
	}
	public abstract String getURL();
	
	public abstract Type getType();
	/**
	 * Post请求
	 */
	public void requestByPost() {
		
		StringBuilder printUrl = new StringBuilder();// 打印请求路径 
		printUrl.append(getURL());
		if(getURL().contains("?")){
			printUrl.append("&");
		}else{
			printUrl.append("?");
		}
		Long current = System.currentTimeMillis() / 1000;
		Long modify = 0l;
		modify = PreferencesUtils.getLong(mContext, "tag");
		Long mTimestamp = current + modify;
		int index = (int) (mTimestamp / 10 % 10);
		mPostValues.put("stamp" , mTimestamp + "");
		if (mPostValues.containsKey("bd_sig"))
		{
			mPostValues.remove("bd_sig");
		}
		Set<Entry<String, String>> entrySet = mPostValues.entrySet();
		// Mac 参数构建macStr
		StringBuilder macStr = new StringBuilder();
		for (Map.Entry<String, String> entry : entrySet){
			String key = entry.getKey();
			String value = entry.getValue();
			if (TextUtils.isEmpty(value)) {
				value = "";
			}
			LogUtils.e("key_value", key + ":" + value);
			macStr.append(key);
			macStr.append("=");
			macStr.append(value);
			macStr.append("&");
			
			printUrl.append(key);
			printUrl.append("=");
			printUrl.append(value);
			printUrl.append("&");

		}
		macStr.deleteCharAt(macStr.length() - 1);
		String token_key = "";
//		token_key = Config.token_key[0];
		token_key = Config.token_key[index];
		macStr.append(token_key);
		String md5Str = MD5Util.getMD5String(macStr.toString());
		
		printUrl.append("bd_sig");
		printUrl.append("=");
		printUrl.append(md5Str);
		LogUtils.e("url", printUrl.toString());
		mPostValues.remove("a");
		mPostValues.remove("m");
		mPostValues.put("bd_sig", md5Str);
		request(Method.POST,getURL());		
	}
	
	/*
	 * Get请求
	 */
	public void requestByGet(){
		StringBuilder printUrl = new StringBuilder();// 打印请求路径 
		printUrl.append(getURL());
		if(getURL().contains("?")){
			printUrl.append("&");
		}else{
			printUrl.append("?");
		}
		Set<Entry<String, String>> entrySet = mPostValues.entrySet();
		for (Map.Entry<String, String> entry : entrySet) {
			String key = entry.getKey();
			String value = entry.getValue();
			if (TextUtils.isEmpty(value)) {
				value = "";
			}
			LogUtils.e("key_value", key + ":" + value);
			
			printUrl.append(key);
			printUrl.append("=");
			printUrl.append(value);
			printUrl.append("&");
		}
		printUrl.deleteCharAt(printUrl.length() - 1);
		String getUrl = printUrl.toString();
		LogUtils.e("url", getUrl);
		mPostValues.clear();
		request(Method.GET, getUrl);
	}
	
	
	private void showNetError(Activity activity){
		 Class clazz = activity.getClass();
		 try {
			java.lang.reflect.Method m1 = clazz.getMethod("showNetErrorDialog");
			m1.invoke(activity); 
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}

	private void request(int method, String url){
		GsonRequest<T> request = new GsonRequest<T>(method, url, mPostValues, getClazz(), getType(), mCallBack, new ErrorListener(){

			@Override
			public void onErrorResponse(VolleyError volleyerror) {
				String msg = volleyerror.getMessage();//网络请求错误提示
				if (!TextUtils.isEmpty(msg))
				{
					mCallBack.onFailure(mContext, msg);
				}else {
					showNetError((Activity)mContext);
				}
				
			}
			
		});
		
		HttpModel.getInstance().add(request, mContext);
	}
	
	public abstract Class<T> getClazz();
}
